package com.pet.order.controller;


import com.pet.api.dto.OrderDetailDTO;
import com.pet.common.domain.Result;
import com.pet.order.domain.dto.PayFormDTO;
import com.pet.order.domain.dto.RefundFormDTO;
import com.pet.order.domain.query.OrderQuery;
import com.pet.order.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author Cookies
 * @since 2024-07-11
 */
@Api(tags = "订单服务接口")
@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
public class OrderController {
    private final IOrderService orderService;

    @ApiOperation("创建订单")
    @PostMapping
    public Result createOrder(@RequestBody List<OrderDetailDTO> detailDTOList) {
        return orderService.createOrder(detailDTOList);
    }

    @ApiOperation("取消订单")
    @PutMapping("/cancel/{orderId}")
    public Result cancelOrder(@PathVariable Long orderId) {
        return orderService.cancelOrder(orderId);
    }

    @GetMapping("/unpaid/{orderId}")
    @ApiOperation("获取未支付订单详情")
    public Result getUnpaidOrderDetail(@PathVariable Long orderId) {
        return orderService.getUnpaidOrderDetail(orderId);
    }

    @PostMapping("/pay")
    @ApiOperation("支付订单")
    public Result payOrder(
            @RequestBody @Validated PayFormDTO payFormDTO) {
        return orderService.payOrder(payFormDTO);
    }

    @PutMapping("/refund")
    @ApiOperation("订单申请退款")
    public Result refundOrder(
            @RequestBody @Validated RefundFormDTO refundFormDTO) {
        return orderService.refundOrder(refundFormDTO);
    }

    @GetMapping("/category")
    @ApiOperation("分类查询订单列表")
    public Result getOrderListByCategory(
            @RequestParam(required = false, defaultValue = "9") Integer type) {
        return orderService.getOrderListByCategory(type);
    }

    @PostMapping("/admin/list")
    @ApiOperation("管理员分页查询订单列表")
    public Result getOrderListByAdmin(@RequestBody OrderQuery orderQuery) {
        return orderService.getOrderListByAdmin(orderQuery);
    }

    @GetMapping("/admin/{id}")
    @ApiOperation("管理员根据订单id查询订单详情")
    public Result getOrderById(@PathVariable Long id) {
        return orderService.getOrderById(id);
    }

    @PutMapping("/admin/ship/{id}")
    @ApiOperation("管理员处理订单发货")
    public Result shipOrder(@PathVariable Long id) {
        return orderService.shipOrder(id);
    }

    @PutMapping("/admin/audit/{id}/{result}")
    @ApiOperation("管理员审核退单申请")
    public Result auditRefund(@PathVariable Long id, @PathVariable Boolean result) {
        return orderService.auditRefund(id, result);
    }

    @PutMapping("/confirmShip/{orderId}")
    @ApiOperation("用户确认收货")
    public Result confirmShip(@PathVariable Long orderId) {
        return orderService.confirmShip(orderId);
    }

    @GetMapping("/orderDetail")
    @ApiOperation(value = "获取对应订单商品详情", hidden = true)
    public List<Integer> getDetailByOrder(@RequestParam Long orderId) {
        return orderService.getDetailByOrder(orderId);
    }

    @PutMapping("/review")
    @ApiOperation(value = "用户评价修改订单状态", hidden = true)
    public void updateOrderByReview(@RequestParam Long orderId) {
        orderService.updateOrderByReview(orderId);
    }
}
